package com.xbts.system.module.payment.utils;


import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.xbts.system.module.payment.kit.params.dto.Withdrawal;
import com.xbts.system.module.payment.setting.AlipayPaymentSetting;


public class AliPayWithdrawalUtil {
    // 支付宝应用appid
    private static String appid = "你的应用appID";

    //支付宝应用私钥
    private static String merchantPrivateKey = "应用私钥";

    //支付宝应用公钥证书路径
    private static String appCertPublicKey = "应用公钥证书路径";

    //支付宝公钥证书路径
    private static String certPublicKey = "公钥证书路径";

    //支付宝根证书路径
    private static String rootCert = "根证书路径";

    /**
     * 支付宝提现功能
     *
     * @param withdrawal 支付宝转账参数实体类
     * @return AlipayFundTransUniTransferResponse response
     * 判断成功失败 response.isSuccess()：true 成功 ，false失败
     * 成功获取交易单号：
     * JSONObject jsonObject = JSONObject.parseObject(response.getBody());
     * String alipay_fund_trans_uni_transfer_response = jsonObject.getString("alipay_fund_trans_uni_transfer_response");
     * JSONObject parseObject = JSONObject.parseObject(alipay_fund_trans_uni_transfer_response);
     * 失败获取错误结果：response.getSubMsg()
     * 成功获取交易单号：parseObject.getString("order_id")
     */
    public static AlipayFundTransUniTransferResponse withdrawal(AlipayPaymentSetting alipayPaymentSetting, Withdrawal withdrawal) {
        AlipayFundTransUniTransferResponse response = null;
        try {
            //
//            String rootPath = AliPayWithdrawalUtil.class.getResource("/").getPath();
//            System.out.println("项目绝对路径" + rootPath);
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
            certAlipayRequest.setAppId(alipayPaymentSetting.getAppId());
            certAlipayRequest.setPrivateKey(alipayPaymentSetting.getPrivateKey());
            certAlipayRequest.setFormat("json");
            certAlipayRequest.setCharset("UTF-8");
            certAlipayRequest.setSignType("RSA2");
            certAlipayRequest.setCertPath(alipayPaymentSetting.getCertPath());
            certAlipayRequest.setAlipayPublicCertPath(alipayPaymentSetting.getAlipayPublicCertPath());
            certAlipayRequest.setRootCertPath(alipayPaymentSetting.getRootCertPath());

//            certAlipayRequest.setCertPath("D:" + alipayPaymentSetting.getCertPath());
//            certAlipayRequest.setAlipayPublicCertPath("D:" + alipayPaymentSetting.getAlipayPublicCertPath());
//            certAlipayRequest.setRootCertPath("D:" + alipayPaymentSetting.getRootCertPath());
            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

//            DefaultAlipayClient alipayClient = AliPayApiConfigKit.getAliPayApiConfig();
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            request.setBizContent("{" +
                    "\"out_biz_no\":\"" + withdrawal.getOutBizNo() + "\"," +
                    "\"trans_amount\":" + withdrawal.getTransAmount() + "," +
                    "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +
                    "\"biz_scene\":\"DIRECT_TRANSFER\"," +
                    "\"order_title\":\"" + withdrawal.getOrderTitle() + "\"," +
                    "\"payee_info\":{" +
                    "\"name\":\"" + withdrawal.getName() + "\"," +
                    "\"identity\":\"" + withdrawal.getIdentity() + "\"," +
                    "\"identity_type\":\"ALIPAY_LOGON_ID\"" +
                    "    }," +
                    "\"remark\":\"" + withdrawal.getRemark() + "\"," +
                    "\"business_params\":\"{\\\"payer_show_name\\\":\\\"迈越\\\"}\"," +
                    "  }");
            response = alipayClient.certificateExecute(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
}